package com.example.demo.controller.admin;

import com.example.demo.entity.sysuser.SysPermission;
import com.example.demo.entity.sysuser.SysRole;
import com.example.demo.entity.sysuser.SysUser;
import com.example.demo.repository.SysUser.SysPermissionRepository;
import com.example.demo.repository.SysUser.SysRoleRepository;
import com.example.demo.service.SysUser.SysUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.List;

@Controller
@RequestMapping("/admin")
public class AdminController {
    @Resource
    private SysUserService adminService;
    @Autowired
    private SysRoleRepository sysRoleRepository;
    @Autowired
    private SysPermissionRepository sysPermissionRepository;


    @GetMapping(value = {"/index","/",""})
    public String index(HttpServletRequest request) {
        return "/admin/index";
    }
    //系统用户操作
    @GetMapping("/user/add")
    public String add() {
        return "/admin/user/add";
    }
    @PostMapping("/user/add")
    public String add(SysUser user) {
        adminService.save(user);
        return "redirect:/admin/user/list";
    }
    @GetMapping("/user/list")
    public String userList(Integer page, Integer size, Model model) {
        if(page==null) page = 0;
        if(size==null) size = 2;
        Page<SysUser> pages = adminService.PageByAdminUser(page,size);
        model.addAttribute("pages", pages);
        System.out.println(pages.getContent()+","+pages.getTotalElements()+","
            +pages.getNumber()+","+pages.getNumberOfElements());
        return "/admin/user/list";
    }
    //系统角色操作
    @GetMapping("/role/add")
    public String addRole() {
        return "/admin/role/add";
    }
    @PostMapping("/role/add")
    public String addRole(SysRole model) {
        String role = "ROLE_" + model.getRole();
        model.setRole(role);
        sysRoleRepository.save(model);
        return "redirect:/admin/role/list";
    }
    @GetMapping("/role/list")
    public String roleList(Integer page, Integer size, Model model) {
        if(page==null) page = 0;//JPA第一页从0开始
        if(size==null) size = 2;
        Pageable pageable =  PageRequest.of(page,size, Sort.Direction.ASC,"id");
        Page<SysRole> pages = sysRoleRepository.findAll(pageable);
        model.addAttribute("pages", pages);

        return "/admin/role/list";
    }
    //系统权限操作
    @GetMapping("/permission/add")
    public String addPermission() {
        return "/admin/permission/add";
    }
    @PostMapping("/permission/add")
    public String addPermission(SysPermission sysPermission, String role) {
        List<SysRole> roles = new ArrayList<>();
        SysRole role1 = sysRoleRepository.findByRole(role);
        roles.add(role1);
        sysPermission.setRoles(roles);
        sysPermissionRepository.save(sysPermission);
        return "redirect:/admin/permission/list";
    }
    @GetMapping("/permission/list")
    public String permissionList(Integer page, Integer size, Model model) {
        if(page==null) page = 0;
        if(size==null) size = 2;
        Pageable pageable =  PageRequest.of(page,size, Sort.Direction.ASC,"id");
        Page<SysPermission> pages = sysPermissionRepository.findAll(pageable);
        model.addAttribute("pages", pages);

        return "/admin/permission/list";
    }
}
